/**
 * @title AbsAttentionAdapter.java
 * @package com.esun.cheetah.view.personal.adapter
 * @description 关注列表的抽象父类
 * @author liugan
 * @date 2012-5-8
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.adapter;

import java.util.ArrayList;

import com.esun.cheetah.R;
import com.esun.cheetah.model.personal.attention.AttentionGroupInfo;
import com.esun.cheetah.model.personal.attention.AttentionGroupInfo.GroupState;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @title AbsAttentionAdapter
 * @description 关注列表的抽象父类
 * @author liugan
 */
public abstract class AbsAttentionAdapter extends BaseExpandableListAdapter {

	public static final int POSITION_RIGHT_NOW = 0;
	public static final int POSITION_FUTURE = 1;
	public static final int POSITION_DONE = 2;

	public static final int GROUP_COUNT = 3;
	/**
	 * 一级列表
	 */
	protected ArrayList<AttentionGroupInfo> mGroupList;
	protected Context context;

	public AbsAttentionAdapter(Context context) {
		this.context = context;
		initGroup();
	}

	/**
	 * @title initGroup
	 * @description 初始化一级列表的数据
	 */
	protected void initGroup() {
		mGroupList = new ArrayList<AttentionGroupInfo>();

		AttentionGroupInfo rightNowGroup = new AttentionGroupInfo();
		rightNowGroup.setTitle("正在进行");
		rightNowGroup.setState(GroupState.uninitialized);
		rightNowGroup.setPosition(POSITION_RIGHT_NOW);
		mGroupList.add(rightNowGroup);

		AttentionGroupInfo futureGroup = new AttentionGroupInfo();
		futureGroup.setTitle("未开赛");
		futureGroup.setState(GroupState.uninitialized);
		futureGroup.setPosition(POSITION_FUTURE);
		mGroupList.add(futureGroup);

		AttentionGroupInfo doneGroup = new AttentionGroupInfo();
		doneGroup.setTitle("已完成");
		doneGroup.setState(GroupState.uninitialized);
		doneGroup.setPosition(POSITION_DONE);
		mGroupList.add(doneGroup);
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		GroupState state = mGroupList.get(groupPosition).getState();
		if (state == GroupState.empty || state == GroupState.uninitialized) {
			return 1;
		}
		return getChildSize(groupPosition);
	}

	/**
	 * @title setGroupState
	 * @description 根据记录条数设置Group的状态
	 * @param size
	 *            二级列表的size
	 * @param index
	 *            group的位置
	 */
	protected void setGroupState(int size, int index) {
		if (size == 0) {
			mGroupList.get(index).setState(GroupState.empty);
		} else if (mGroupList.get(index).getState() != GroupState.normal) {
			mGroupList.get(index).setState(GroupState.normal);
		}

	}

	/**
	 * @title setGroupState
	 * @description 设置Group的状态
	 * @param index
	 *            group的位置
	 * @param state
	 *            需要设置的状态
	 */
	public void setGroupState(int index, GroupState state) {
		mGroupList.get(index).setState(state);
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder groupViewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.attention_list_title, null);
			groupViewHolder = new GroupViewHolder();
			groupViewHolder.tvTitle = (TextView) convertView
					.findViewById(R.id.tv_attention_list_title);
			groupViewHolder.ivIndicater = (ImageView) convertView
					.findViewById(R.id.iv_attention_list_title_indicator);
			convertView.setTag(groupViewHolder);
		} else {
			groupViewHolder = (GroupViewHolder) convertView.getTag();
		}
		groupViewHolder.tvTitle.setText(mGroupList.get(groupPosition)
				.getTitle());
		if (!isExpanded) {
			groupViewHolder.ivIndicater
					.setBackgroundResource(R.drawable.icon_default_unfold);
		} else {
			groupViewHolder.ivIndicater
					.setBackgroundResource(R.drawable.icon_default_packup);
		}
		return convertView;
	}

	/**
	 * @title getChildSize
	 * @description 获得该一级列表当前项下的二级列表的尺寸
	 * @param groupPosition
	 * @return
	 */
	public abstract int getChildSize(int groupPosition);

	@Override
	public int getGroupCount() {
		return mGroupList.size();
	}

	@Override
	public AttentionGroupInfo getGroup(int groupPosition) {
		return mGroupList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		if (mGroupList.get(groupPosition).getState() == GroupState.normal) {
			return true;
		}
		return false;
	}

	public ArrayList<AttentionGroupInfo> getGroupList() {
		return mGroupList;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	/**
	 * @title GroupViewHolder
	 * @description 一级列表的ViewHolder
	 * @author liugan
	 */
	class GroupViewHolder {
		TextView tvTitle;
		ImageView ivIndicater;
	}

}
